home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: Thu, 29 Feb 1996 20:46:43 GMT
- Organization: Netcom
- Message-ID: <31361078.89905707@nntp.ix.netcom.com>
- References: <4gqpa1$3h9@alcor.usc.edu> <1996Feb26.211807.28858@isac.hces.com> <31331a38.54160408@nntp.ix.netcom.com> <1996Feb28.195423.10465@isac.hces.com>
- NNTP-Posting-Host: ix-dc12-23.ix.netcom.com
- X-NETCOM-Date: Thu Feb 29 12:46:44 PM PST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- gg@isac.hces.com (Greg Goodrich) wrote:
-
- > Mike Rubenstein (miker3@ix.netcom.com) wrote:
- > : gg@isac.hces.com (Greg Goodrich) wrote:
- >
- > : > Abu Wawda (wawda@alcor.usc.edu) wrote:
- > : > : I'm having trouble understanding what the address of a static array
- > : > : is. For example, if I declare a variable called myarray as:
- > : > : char myarray[10];
- > : > : then what could &myarray possibly mean? myarray is not a pointer, so
- > : > : &myarray could not possibly be the address of the variable myarray
- > : > : (like it would be if I did char* myarray and then asked for &myarray).
- > : >
- > : > : Functions such as scanf() allow the following:
- > : >
- > : > : char myarray[10];
- > : >
- > : > : scanf("%s",&myarray);
- > : >
- > : > : but I don't understand what scanf() could possibly be taking in the
- > : > : second parameter. It can't be: char** since myarray is not a
- > : > : pointer. I CAN understand how the following would work:
- > : >
- > : > This is because C treats the occurrence of array names as the address of
- > : > the array. Therefore the following are equivalent:
- > : >
- > : > scanf("%s", myarray);
- > : > scanf("%s", &myarray);
- > : >
- > : > The thing is, the address is implied when you use an array name. This
- > : > is not so for pointers. I am not sure why C was incorporated in this
- > : > way. In my humble opinion, it would make it easier and clearer to force
- > : > the programmer to put the & in front of array names, the same as you
- > : > have to do for any other storage class.
- >
- > : No. The are not equivalent. The first is legal and the second is
- > : not.
- >
- > : The %s format item in scanf expects a pointer to char. myarray is
- > : converted to a pointer to char so it is legal. &myarray is a pointer
- > : to array of 10 char and is not converted. This results in undefined
- > : behavior.
- >
- > : In many implementations pointer to char and pointer to array of 10
- > : char have the same representation and this will work properly, but
- > : this is not required by the standard.
- >
- > I would like to see an example of how this could be implemented to fail.
- > They both point to the exact same memory address, the only difference
- > being the datatype of the value, which can be type casted if necessary.
- > If you have an array, the address of the array is also the address of
- > the first member of the array, because the array itself is not a
- > pointer, but a reference. The same goes for a structure. The address
- > of a struct is the same as the address of the first member of the
- > struct, but of different data type. One is pointer to struct and the
- > other is pointer to whatever the first member is declared as, but they
- > are both pointers, and therefore are the same size.
-
- Let me urge you to stop asking such questions. The fact that the
- standard says it is illegal is sufficient. Guessing whether there is
- an implementation in which it fails is fruitless since even if there
- is not someone might write one tomorrow.
-
- In any case, I find it hard to justify code based on lack of
- imagination.
-
- The construction is likely to fail in any implementation in which
- sizeof(char*) != sizeof(char (*) 10) or in which they have different
- representations.
-
- Michael M Rubenstein
-